package conson.tech.manage.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.manage.api.app.dto.AppAdvertDTO;
import conson.tech.manage.app.entity.pojo.AppAdvert;
import conson.tech.manage.app.entity.pojo.AppMaintainConfig;
import conson.tech.manage.app.entity.vo.AppAdvertVo;
import conson.tech.manage.app.entity.vo.PageVo;
import conson.tech.manage.app.mapper.AppAdvertMapper;
import conson.tech.manage.app.mapper.AppMaintainConfigMapper;
import conson.tech.manage.app.service.AppAdvertService;
import conson.tech.manage.common.util.DateUtil;
import conson.tech.manage.common.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AppAdvertServiceImpl
 * Description: app广告服务实现类
 * Author: HXL
 * Date: 2020/4/22 6:58 下午
 * Version: 1.0
 */
@Service("appAdvertService")
public class AppAdvertServiceImpl implements AppAdvertService {
    @Autowired
    private AppAdvertMapper appAdvertMapper;

    @Autowired
    private AppMaintainConfigMapper appMaintainConfigMapper;
    @Transactional
    @Override
    public int createAdvert(AppAdvertVo vo) {
        AppAdvert appAdvert = new AppAdvert();
        BeanUtils.copyProperties(vo, appAdvert);
        if (appAdvert.getNavType() != null && appAdvert.getNavType() == 1 ){
            //去查询ID页
            AppMaintainConfig appMaintainConfig = appMaintainConfigMapper.selectOne(Wrappers.<AppMaintainConfig>query().lambda().eq(AppMaintainConfig::getAppId, appAdvert.getAppId()));

            //设置参数
            appAdvert = this.buildAppAdvert(appAdvert, appMaintainConfig);
        }
        if (!StringUtils.isEmpty(vo.getAdStartTime())) {
            appAdvert.setStartTime(DateUtil.getLocalDateTime(vo.getAdStartTime()));
        }
        if (!StringUtils.isEmpty(vo.getAdEndTime())) {
            appAdvert.setEndTime(DateUtil.getLocalDateTime(vo.getAdEndTime()));
        }
        appAdvert.setCreatedTime(LocalDateTime.now());
        appAdvert.setUpdatedTime(LocalDateTime.now());
        appAdvert.setDelFlag(1);
        appAdvert.setOpen(1);
        return appAdvertMapper.insert(appAdvert);
    }
    @Transactional
    @Override
    public int updateAdvert(AppAdvertVo vo) {
        AppAdvert appAdvert = new AppAdvert();
        BeanUtils.copyProperties(vo, appAdvert);
        if (appAdvert.getNavType() != null && appAdvert.getNavType() == 1 && StrUtil.isNotEmpty(appAdvert.getAppId())){

            //去查询ID页
            AppMaintainConfig appMaintainConfig = appMaintainConfigMapper.selectOne(Wrappers.<AppMaintainConfig>query().lambda().eq(AppMaintainConfig::getAppId, appAdvert.getAppId()));

            //设置参数
            appAdvert = this.buildAppAdvert(appAdvert, appMaintainConfig);
        }
        if (!StringUtils.isEmpty(vo.getAdStartTime())) {
            appAdvert.setStartTime(DateUtil.getLocalDateTime(vo.getAdStartTime()));
        }
        if (!StringUtils.isEmpty(vo.getAdEndTime())) {
            appAdvert.setEndTime(DateUtil.getLocalDateTime(vo.getAdEndTime()));
        }
        appAdvert.setUpdatedTime(LocalDateTime.now());
        return appAdvertMapper.updateById(appAdvert);
    }

    @Override
    public Page<AppAdvertVo> queryAdvertPage(PageVo vo) {
        Page<AppAdvert> page = new Page();
        page.setCurrent(vo.getPageIndex());
        page.setSize(vo.getPageSize());
        Page<AppAdvert> appAdvertPage = appAdvertMapper.queryAdvertPage(page,vo.getId());
        Page<AppAdvertVo> adPage = new Page<>();
        List<AppAdvertVo> appAdvertVoList = new ArrayList<>();
        if (appAdvertPage != null) {
            List<AppAdvert> records = appAdvertPage.getRecords();
            if (!CollectionUtils.isEmpty(records)){
                for (AppAdvert record : records) {
                    AppAdvertVo advertVo = new AppAdvertVo();
                    BeanUtils.copyProperties(record, advertVo);
                    if (record.getEndTime() != null) {
                        advertVo.setAdEndTime(DateUtil.getLocalDateTimeStr(record.getEndTime()));
                    }
                    if (record.getStartTime() != null) {
                        advertVo.setAdStartTime(DateUtil.getLocalDateTimeStr(record.getStartTime()));
                    }
                    appAdvertVoList.add(advertVo);
                }
            }
           BeanUtils.copyProperties(appAdvertPage, adPage);
            //return EntityUtils.pageToPage(dtoPage, AppAdvertVo.class);
        }
        adPage.setRecords(appAdvertVoList);
        return adPage;
    }
    @Transactional
    @Override
    public int deleteAdvertById(Long id) {
        AppAdvert appAdvert = new AppAdvert();
        appAdvert.setDelFlag(0);
        appAdvert.setId(id);
        appAdvert.setUpdatedTime(LocalDateTime.now());
        return appAdvertMapper.updateById(appAdvert);
    }

    @Override
    public List<AppAdvertDTO> queryAdvertListByPlaceId(Long placeId,Integer versionValue) {

        List<AppAdvert> list = appAdvertMapper.queryAdvertListByPlaceId(placeId,versionValue);
        if (!CollectionUtils.isEmpty(list)){
           return EntityUtils.listToList(list, AppAdvertDTO.class);
        }
        return new ArrayList<>();
    }



    private AppAdvert buildAppAdvert(AppAdvert appAdvert, AppMaintainConfig appMaintainConfig){
        if (appMaintainConfig != null) {
            //appAdvert.setName(appMaintainConfig.getAppName());
            appAdvert.setAppId(appMaintainConfig.getAppId());
            appAdvert.setIsTitle(appMaintainConfig.getIsTitle());
            appAdvert.setAppType(appMaintainConfig.getAppType());
            appAdvert.setIosGoPage(appMaintainConfig.getIosGoPage());
            appAdvert.setAndroidGoPage(appMaintainConfig.getAndroidGoPage());
            appAdvert.setAppStartUrl(appMaintainConfig.getAppStartUrl());
            appAdvert.setGroupInfoId(appMaintainConfig.getGroupInfoId());
        }
        return appAdvert;
    }


}
